
// 1. 定义所有接口的Mock配置表（保持不变）
const mockConfig = {
  '/api/rec': {
    method: 'GET',
    data: {
      code: 1,
      msg: 'success',
      data: [{
        id: 1,
        url: '/static/images/rec/rec01.png',
        height: '400',
        recName: 'PHP123123123123123123123123',
        tags: ['计算机', '速成']
      },
      {
        id: 2,
        url: '/static/images/rec/rec02.png',
        height: '400',
        recName: 'Java + AI',
        tags: ['计算机', '速成']
      },
      {
        id: 3,
        url: '/static/images/rec/rec03.png',
        height: '400',
        recName: '人工智能',
        tags: ['计算机', '速成']
      },
      {
        id: 4,
        url: '/static/images/rec/rec04.png',
        height: '270',
        recName: '人工智能',
        tags: ['计算机', '速成']
      },
      {
        id: 5,
        url: '/static/images/rec/rec05.png',
        height: '280',
        recName: '人工智能',
        tags: ['计算机', '速成']
      },
      {
        id: 6,
        url: '/static/images/rec/rec06.png',
        height: '400',
        recName: '人工智能',
        tags: ['计算机', '速成']
      },
      {
        id: 7,
        url: '/static/images/rec/rec07.png',
        height: '200',
        recName: '人工智能',
        tags: ['计算机', '速成']
      },
      {
        id: 8,
        url: '/static/images/rec/rec08.png',
        height: '400',
        recName: '人工智能',
        tags: ['计算机', '速成']
      },
      {
        id: 9,
        url: '/static/images/rec/rec09.png',
        height: '220',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      {
        id: 10,
        url: '/static/images/rec/rec10.png',
        height: '220',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      {
        id: 11,
        url: '/static/images/rec/rec11.png',
        height: '220',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      {
        id: 12,
        url: '/static/images/rec/rec12.png',
        height: '320',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      {
        id: 13,
        url: '/static/images/rec/rec13.png',
        height: '320',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      {
        id: 14,
        url: '/static/images/rec/rec14.png',
        height: '400',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      {
        id: 15,
        url: '/static/images/rec/rec15.png',
        height: '220',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      {
        id: 16,
        url: '/static/images/rec/rec16.png',
        height: '220',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      {
        id: 17,
        url: '/static/images/rec/rec17.png',
        height: '320',
        recName: 'Python',
        tags: ['计算机', '速成']
      },
      ]
    },
    
  },
  // 其他接口...
}

// 2. 根据环境选择不同的拦截方式
if (typeof wx !== 'undefined' && wx.request) {
  // 微信小程序环境：拦截wx.request
  const originalRequest = wx.request
  wx.request = function(options) {
    const method = (options.method || 'GET').toUpperCase()
    for (const [url, config] of Object.entries(mockConfig)) {
      if (options.url === url && config.method === method) {
        console.log(`Mock 拦截成功(wx): ${method} ${url}`)
        setTimeout(() => {
          if (options.success) {
            options.success({ data: config.data })  // 返回正确的模拟数据
          }
          if (options.complete) {
            options.complete({ data: config.data })  // 确保 complete 也执行
          }
        }, 100)
        return // 直接返回，不再需要 abort
      }
    }
    return originalRequest.call(wx, options) // 若未匹配到，使用原始请求逻辑
  }
} else if (typeof XMLHttpRequest !== 'undefined') {
  // 浏览器环境：拦截XMLHttpRequest
  const originalXHR = XMLHttpRequest
  XMLHttpRequest = function() {
    const xhr = new originalXHR()
    const originalOpen = xhr.open

    xhr.open = function(method, url) {
      // 保存请求信息，用于后续拦截
      this._mockMethod = method.toUpperCase()
      this._mockUrl = url
      return originalOpen.apply(this, arguments)
    }

    // 重写send方法，返回Mock数据
    const originalSend = xhr.send
    xhr.send = function() {
      const method = this._mockMethod
      const url = this._mockUrl.replace(/^https?:\/\/[^\/]+/, '') // 移除域名，只保留路径

      for (const [mockUrl, config] of Object.entries(mockConfig)) {
        if (url === mockUrl && config.method === method) {
          console.log(`Mock 拦截成功(浏览器): ${method} ${url}`)
          // 模拟异步返回
          setTimeout(() => {
            this.status = 200
            this.readyState = 4
            // 确保是一个有效的JSON字符串
            this.responseText = JSON.stringify(config.data)
            console.log("模拟返回数据: ", this.responseText) // 调试输出
            // 触发onload事件，让浏览器认为请求已完成
            if (this.onload) {
              this.onload() // 确保调用时已经设置了正确的 responseText
            }
          }, 100)
          return // 直接返回，不需要再做abort
        }
      }

      // 未匹配的请求走原始逻辑
      originalSend.apply(this, arguments)
    }

    return xhr
  }
}
